package ru.spb.osll.myfit.server.handler;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;

import ru.spb.osll.myfit.common.Define;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.server.parser.PageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.easymock.EasyMock.createMock;
import static org.junit.Assert.assertEquals;

/**
 * Created by IntelliJ IDEA.
 * User: Vassar
 * Date: 26.05.2011
 * Time: 20:36:04
 * To change this template use File | Settings | File Templates.
 */
public class ParentVariablesCreatorTest {
  private ParentVariablesCreator creator;
  private PageLoader loader;
  private String CONTENT = "content.txt";

  @Before
  public void setUp() throws SecurityException, NoSuchMethodException {
      loader = createMock(PageLoader.class);
      creator = new ParentVariablesCreator(loader);
  }

  @Test
  public void getParentVariablesTest()throws IOException {
    String root = "ROOT";
//    String fileName = root + File.separator + "MyTest" + File.separator + "MyTest2" + File.separator +
//            "MyTest3" + File.separator + CONTENT;

    List<String> list = new ArrayList<String>();
    list.add(root + File.separator + CONTENT);
    list.add(root + File.separator + "MyTest" + File.separator + "MyTest2" + CONTENT);

    Page myTestPage = new Page();
    myTestPage.insert(new Define("var1", "var1"));
    myTestPage.insert(new Define("var2", "var2"));
    
    Page myTestPage2 = new Page();
    myTestPage2.insert(new Define("var2", "qwe"));
    myTestPage2.insert(new Define("var3", "var3"));
    
    EasyMock.expect(loader.loadPage(list.get(0))).andReturn(myTestPage);
    EasyMock.expect(loader.loadPage(list.get(1))).andReturn(myTestPage2);
    EasyMock.replay(loader);

    HashMap<String, String> parentVariables = creator.getParentVariables(list, root);
    EasyMock.verify(loader);

    assertEquals(3, parentVariables.size());
    assertEquals("var1", parentVariables.get("var1"));
    assertEquals("qwe", parentVariables.get("var2"));
    assertEquals("var3", parentVariables.get("var3"));
  }

  @Test
  public void loadParentVariablesWithIncludedPageTest() throws IOException {
    String root = "ROOT";
    String fileName = root + File.separator + "path1";
    String path2 = "path2";

    Page page1 = new Page();
    page1.insert(new Define("var1","var1"));
    page1.insert(new Define("var2","var2"));
    
    Page page2 = new Page();
    page2.insert(new Define("var2","qwe"));
    page2.insert(new Define("var3","var3"));
    
    
    Include include = new Include(path2, page2);
    page1.insert(include);

    EasyMock.expect(loader.loadPage(fileName)).andReturn(page1);
    EasyMock.expect(loader.loadPage(PageUtils.getFullPathToContent(root, path2))).andReturn(page2);
    EasyMock.replay(loader);

    HashMap<String, String> map = creator.loadPageWithoutParentVariables(fileName, root);

    assertEquals(3, map.size());
    assertEquals("var1", map.get("var1"));
    assertEquals("qwe", map.get("var2"));
    assertEquals("var3", map.get("var3"));
  }
}
